home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / symfile.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  28KB  |  1,019 lines

  1. /* Generic symbol file reading for the GNU debugger, GDB.
  2.    Copyright 1990, 1991 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support, using pieces from other GDB modules.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include "defs.h"
  23. #include "symtab.h"
  24. #include "param.h"
  25. #include "gdbcore.h"
  26. #include "frame.h"
  27. #include "target.h"
  28. #include "value.h"
  29. #include "symfile.h"
  30. #include "gdbcmd.h"
  31. #include "breakpoint.h"
  32.  
  33. #include <obstack.h>
  34. #include <assert.h>
  35.  
  36. #include <sys/types.h>
  37. #include <fcntl.h>
  38. #include <string.h>
  39. #include <sys/stat.h>
  40.  
  41. extern int info_verbose;
  42.  
  43. extern void qsort ();
  44. extern char *getenv ();
  45. extern char *rindex ();
  46.  
  47. /* Functions this file defines */
  48. static bfd *symfile_open();
  49. static struct sym_fns *symfile_init();
  50. static void clear_symtab_users_once();
  51.  
  52. /* List of all available sym_fns.  */
  53.  
  54. struct sym_fns *symtab_fns = NULL;
  55.  
  56. /* Saves the sym_fns of the current symbol table, so we can call
  57.    the right XXX_new_init function when we free it.  FIXME.  This
  58.    should be extended to calling the new_init function for each
  59.    existing symtab or psymtab, since the main symbol file and 
  60.    subsequent added symbol files can have different types.  */
  61.  
  62. static struct sym_fns *symfile_fns;
  63.  
  64. /* Allocate an obstack to hold objects that should be freed
  65.    when we load a new symbol table.
  66.    This includes the symbols made by dbxread
  67.    and the types that are not permanent.  */
  68.  
  69. struct obstack obstack1;
  70.  
  71. struct obstack *symbol_obstack = &obstack1;
  72.  
  73. /* This obstack will be used for partial_symbol objects.  It can
  74.    probably actually be the same as the symbol_obstack above, but I'd
  75.    like to keep them seperate for now.  If I want to later, I'll
  76.    replace one with the other.  */
  77.  
  78. struct obstack obstack2;
  79.  
  80. struct obstack *psymbol_obstack = &obstack2;
  81.  
  82. /* File name symbols were loaded from.  */
  83.  
  84. char *symfile = 0;
  85.  
  86. /* The modification date of the file when they were loaded.  */
  87.  
  88. long /* really time_t */ symfile_mtime = 0;
  89.  
  90. /* Structures with which to manage partial symbol allocation.  */
  91.  
  92. struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
  93.  
  94. /* Flag for whether user will be reloading symbols multiple times.
  95.    Defaults to ON for VxWorks, otherwise OFF.  */
  96.  
  97. #ifdef SYMBOL_RELOADING_DEFAULT
  98. int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
  99. #else
  100. int symbol_reloading = 0;
  101. #endif
  102.  
  103. /* Structure to manage complaints about symbol file contents.  */
  104.  
  105. struct complaint complaint_root[1] = {
  106.   {(char *)0, 0, complaint_root},
  107. };
  108.  
  109. /* Some actual complaints.  */
  110.  
  111. struct complaint oldsyms_complaint = {
  112.     "Replacing old symbols for `%s'", 0, 0 };
  113.  
  114. struct complaint empty_symtab_complaint = {
  115.     "Empty symbol table found for `%s'", 0, 0 };
  116.  
  117.  
  118. /* In the following sort, we always make sure that
  119.    register debug symbol declarations always come before regular
  120.    debug symbol declarations (as might happen when parameters are
  121.    then put into registers by the compiler).  */
  122.  
  123. static int
  124. compare_symbols (s1, s2)
  125.      struct symbol **s1, **s2;
  126. {
  127.   register int namediff;
  128.  
  129.   /* Compare the initial characters.  */
  130.   namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
  131.   if (namediff != 0) return namediff;
  132.  
  133.   /* If they match, compare the rest of the names.  */
  134.   namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
  135.   if (namediff != 0) return namediff;
  136.  
  137.   /* For symbols of the same name, registers should come first.  */
  138.   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
  139.       - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
  140. }
  141.  
  142. /* Call sort_block_syms to sort alphabetically the symbols of one block.  */
  143.  
  144. void
  145. sort_block_syms (b)
  146.      register struct block *b;
  147. {
  148.   qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
  149.      sizeof (struct symbol *), compare_symbols);
  150. }
  151.  
  152. /* Call sort_symtab_syms to sort alphabetically
  153.    the symbols of each block of one symtab.  */
  154.  
  155. void
  156. sort_symtab_syms (s)
  157.      register struct symtab *s;
  158. {
  159.   register struct blockvector *bv = BLOCKVECTOR (s);
  160.   int nbl = BLOCKVECTOR_NBLOCKS (bv);
  161.   int i;
  162.   register struct block *b;
  163.  
  164.   for (i = 0; i < nbl; i++)
  165.     {
  166.       b = BLOCKVECTOR_BLOCK (bv, i);
  167.       if (BLOCK_SHOULD_SORT (b))
  168.     sort_block_syms (b);
  169.     }
  170. }
  171.  
  172. void
  173. sort_all_symtab_syms ()
  174. {
  175.   register struct symtab *s;
  176.  
  177.   for (s = symtab_list; s; s = s->next)
  178.     {
  179.       sort_symtab_syms (s);
  180.     }
  181. }
  182.  
  183. /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
  184.    (and add a null character at the end in the copy).
  185.    Returns the address of the copy.  */
  186.  
  187. char *
  188. obsavestring (ptr, size)
  189.      char *ptr;
  190.      int size;
  191. {
  192.   register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
  193.   /* Open-coded bcopy--saves function call time.
  194.      These strings are usually short.  */
  195.   {
  196.     register char *p1 = ptr;
  197.     register char *p2 = p;
  198.     char *end = ptr + size;
  199.     while (p1 != end)
  200.       *p2++ = *p1++;
  201.   }
  202.   p[size] = 0;
  203.   return p;
  204. }
  205.  
  206. /* Concatenate strings S1, S2 and S3; return the new string.
  207.    Space is found in the symbol_obstack.  */
  208.  
  209. char *
  210. obconcat (s1, s2, s3)
  211.      char *s1, *s2, *s3;
  212. {
  213.   register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
  214.   register char *val = (char *) obstack_alloc (symbol_obstack, len);
  215.   strcpy (val, s1);
  216.   strcat (val, s2);
  217.   strcat (val, s3);
  218.   return val;
  219. }
  220.  
  221. /* Accumulate the misc functions in bunches of 127.
  222.    At the end, copy them all into one newly allocated structure.  */
  223.  
  224. #define MISC_BUNCH_SIZE 127
  225.  
  226. struct misc_bunch
  227. {
  228.   struct misc_bunch *next;
  229.   struct misc_function contents[MISC_BUNCH_SIZE];
  230. };
  231.  
  232. /* Bunch currently being filled up.
  233.    The next field points to chain of filled bunches.  */
  234.  
  235. static struct misc_bunch *misc_bunch;
  236.  
  237. /* Number of slots filled in current bunch.  */
  238.  
  239. static int misc_bunch_index;
  240.  
  241. /* Total number of misc functions recorded so far.  */
  242.  
  243. static int misc_count;
  244.  
  245. void
  246. init_misc_bunches ()
  247. {
  248.   misc_count = 0;
  249.   misc_bunch = 0;
  250.   misc_bunch_index = MISC_BUNCH_SIZE;
  251. }
  252.  
  253. void
  254. prim_record_misc_function (name, address, misc_type)
  255.      char *name;
  256.      CORE_ADDR address;
  257.      enum misc_function_type misc_type;
  258. {
  259.   register struct misc_bunch *new;
  260.  
  261.   if (misc_bunch_index == MISC_BUNCH_SIZE)
  262.     {
  263.       new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
  264.       misc_bunch_index = 0;
  265.       new->next = misc_bunch;
  266.       misc_bunch = new;
  267.     }
  268.   misc_bunch->contents[misc_bunch_index].name = name;
  269.   misc_bunch->contents[misc_bunch_index].address = address;
  270.   misc_bunch->contents[misc_bunch_index].type = misc_type;
  271.   misc_bunch->contents[misc_bunch_index].misc_info = 0;
  272.   misc_bunch_index++;
  273.   misc_count++;
  274. }
  275.  
  276. static int
  277. compare_misc_functions (fn1, fn2)
  278.      struct misc_function *fn1, *fn2;
  279. {
  280.   /* Return a signed result based on unsigned comparisons
  281.      so that we sort into unsigned numeric order.  */
  282.   if (fn1->address < fn2->address)
  283.     return -1;
  284.   if (fn1->address > fn2->address)
  285.     return 1;
  286.   return 0;
  287. }
  288.  
  289. /* ARGSUSED */
  290. void
  291. discard_misc_bunches (foo)
  292.      int foo;
  293. {
  294.   register struct misc_bunch *next;
  295.  
  296.   while (misc_bunch)
  297.     {
  298.       next = misc_bunch->next;
  299.       free (misc_bunch);
  300.       misc_bunch = next;
  301.     }
  302. }
  303.  
  304. /* INCLINK nonzero means bunches are from an incrementally-linked file.
  305.    Add them to the existing bunches.
  306.    Otherwise INCLINK is zero, and we start from scratch. */
  307. void
  308. condense_misc_bunches (inclink)
  309.      int inclink;
  310. {
  311.   register int i, j;
  312.   register struct misc_bunch *bunch;
  313.  
  314.   if (inclink)
  315.     {
  316.       misc_function_vector
  317.     = (struct misc_function *)
  318.       xrealloc (misc_function_vector, (misc_count + misc_function_count)
  319.             * sizeof (struct misc_function));
  320.       j = misc_function_count;
  321.     }
  322.   else
  323.     {
  324.       misc_function_vector
  325.     = (struct misc_function *)
  326.       xmalloc (misc_count * sizeof (struct misc_function));
  327.       j = 0;
  328.     }
  329.  
  330.   bunch = misc_bunch;
  331.   while (bunch)
  332.     {
  333.       for (i = 0; i < misc_bunch_index; i++, j++)
  334.         {
  335.       misc_function_vector[j] = bunch->contents[i];
  336. #ifdef NAMES_HAVE_UNDERSCORE
  337.       if (misc_function_vector[j].name[0] == '_')
  338.           misc_function_vector[j].name++;
  339. #endif
  340.     }
  341.       bunch = bunch->next;
  342.       misc_bunch_index = MISC_BUNCH_SIZE;
  343.     }
  344.  
  345.   if (misc_function_count + misc_count != j)        /* DEBUG */
  346.     printf_filtered ("Function counts are off!  %d + %d != %d\n",
  347.       misc_function_count, misc_count, j);
  348.  
  349.   misc_function_count = j;
  350.  
  351.   /* Sort the misc functions by address.  */
  352.  
  353.   qsort (misc_function_vector, misc_function_count,
  354.      sizeof (struct misc_function),
  355.      compare_misc_functions);
  356. }
  357.  
  358.  
  359. /* Get the symbol table that corresponds to a partial_symtab.
  360.    This is fast after the first time you do it.  In fact, there
  361.    is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
  362.    case inline.  */
  363.  
  364. struct symtab *
  365. psymtab_to_symtab (pst)
  366.      register struct partial_symtab *pst;
  367. {
  368.   /* If it's been looked up before, return it. */
  369.   if (pst->symtab)
  370.     return pst->symtab;
  371.  
  372.   /* If it has not yet been read in, read it.  */
  373.   if (!pst->readin)
  374.     { 
  375.       (*pst->read_symtab) (pst);
  376.     }
  377.  
  378.   return pst->symtab;
  379. }
  380.  
  381. /* Process a symbol file, as either the main file or as a dynamically
  382.    loaded file.
  383.  
  384.    NAME is the file name (which will be tilde-expanded and made
  385.    absolute herein) (but we don't free or modify NAME itself).
  386.    FROM_TTY says how verbose to be.  MAINLINE specifies whether this
  387.    is the main symbol file, or whether it's an extra symbol file such
  388.    as dynamically loaded code.  If !mainline, ADDR is the address
  389.    where the text segment was loaded.  */
  390.  
  391. void
  392. symbol_file_add (name, from_tty, addr, mainline)
  393.      char *name;
  394.      int from_tty;
  395.      CORE_ADDR addr;
  396.      int mainline;
  397. {
  398.   bfd *sym_bfd;
  399.   asection *text_sect;
  400.   struct sym_fns *sf;
  401.   char *realname;
  402.  
  403.   sym_bfd = symfile_open (name);
  404.  
  405.   entry_point = bfd_get_start_address (sym_bfd);
  406.  
  407.   if (mainline)
  408.     symfile_mtime = bfd_get_mtime (sym_bfd);
  409.  
  410.   /* There is a distinction between having no symbol table
  411.      (we refuse to read the file, leaving the old set of symbols around)
  412.      and having no debugging symbols in your symbol table (we read
  413.      the file and end up with a mostly empty symbol table).  */
  414.  
  415.   if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
  416.     {
  417.       error ("%s has no symbol-table", name);
  418.     }
  419.  
  420.   if ((symtab_list || partial_symtab_list)
  421.       && mainline
  422.       && from_tty
  423.       && !query ("Load new symbol table from \"%s\"? ", name))
  424.     error ("Not confirmed.");
  425.  
  426.   if (from_tty)
  427.     {
  428.       printf_filtered ("Reading symbols from %s...", name);
  429.       wrap_here ("");
  430.       fflush (stdout);
  431.     }
  432.  
  433.   sf = symfile_init (sym_bfd);
  434.   realname = bfd_get_filename (sym_bfd);
  435.   realname = savestring (realname, strlen (realname));
  436.   /* FIXME, this probably creates a storage leak... */
  437.  
  438.   if (mainline) 
  439.     {
  440.       /* Since no error yet, throw away the old symbol table.  */
  441.  
  442.       if (symfile)
  443.     free (symfile);
  444.       symfile = 0;
  445.       free_all_symtabs ();
  446.       free_all_psymtabs ();
  447.  
  448.       (*sf->sym_new_init) ();
  449.  
  450.       /* For mainline, caller didn't know the specified address of the
  451.          text section.  We fix that here.  */
  452.       text_sect = bfd_get_section_by_name (sym_bfd, ".text");
  453.       addr = bfd_section_vma (sym_bfd, text_sect);
  454.     }
  455.  
  456.   clear_complaints();    /* Allow complaints to appear for this new file. */
  457.  
  458.   (*sf->sym_read) (sf, addr, mainline);
  459.  
  460.   /* Don't allow char * to have a typename (else would get caddr_t.)  */
  461.   /* Ditto void *.  FIXME should do this for all the builtin types.  */
  462.  
  463.   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
  464.   TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
  465.  
  466.   if (mainline)
  467.     {
  468.       /* OK, make it the "real" symbol file.  */
  469.       symfile = realname;
  470.       symfile_fns = sf;
  471.     }
  472.  
  473.   /* If we have wiped out any old symbol tables, clean up.  */
  474.   clear_symtab_users_once ();
  475.  
  476.   if (from_tty)
  477.     {
  478.       printf_filtered ("done.\n");
  479.       fflush (stdout);
  480.     }
  481. }
  482.  
  483. /* This is the symbol-file command.  Read the file, analyze its symbols,
  484.    and add a struct symtab to symtab_list.  */
  485.  
  486. void
  487. symbol_file_command (name, from_tty)
  488.      char *name;
  489.      int from_tty;
  490. {
  491.  
  492.   dont_repeat ();
  493.  
  494.   if (name == 0)
  495.     {
  496.       if ((symtab_list || partial_symtab_list)
  497.       && from_tty
  498.       && !query ("Discard symbol table from `%s'? ", symfile))
  499.     error ("Not confirmed.");
  500.       if (symfile)
  501.     free (symfile);
  502.       symfile = 0;
  503.       free_all_symtabs ();
  504.       free_all_psymtabs ();
  505.       /* FIXME, this does not account for the main file and subsequent
  506.          files (shared libs, dynloads, etc) having different formats. 
  507.          It only calls the cleanup routine for the main file's format.  */
  508.       if (symfile_fns) {
  509.         (*symfile_fns->sym_new_init) ();
  510.         free (symfile_fns);
  511.         symfile_fns = 0;
  512.       }
  513.       return;
  514.     }
  515.  
  516.   /* Getting new symbols may change our opinion about what is
  517.      frameless.  */
  518.   reinit_frame_cache ();
  519.  
  520.   symbol_file_add (name, from_tty, (CORE_ADDR)0, 1);
  521. }
  522.  
  523. /* Open NAME and hand it off to BFD for preliminary analysis.  Result
  524.    is a BFD *, which includes a new copy of NAME dynamically allocated
  525.    (which will be freed by the cleanup chain).  In case of trouble,
  526.    error() is called.  */
  527.  
  528. static bfd *
  529. symfile_open (name)
  530.      char *name;
  531. {
  532.   bfd *sym_bfd;
  533.   int desc;
  534.   char *absolute_name;
  535.  
  536.   name = tilde_expand (name);
  537.   make_cleanup (free, name);
  538.  
  539.   desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
  540.   if (desc < 0)
  541.     perror_with_name (name);
  542.   else
  543.     {
  544.       make_cleanup (free, absolute_name);
  545.       name = absolute_name;
  546.     }
  547.  
  548.   sym_bfd = bfd_fdopenr (name, NULL, desc);
  549.   if (!sym_bfd)
  550.     {
  551.       close (desc);
  552.       error ("Could not open `%s' to read symbols: %s",
  553.          name, bfd_errmsg (bfd_error));
  554.     }
  555.   make_cleanup (bfd_close, sym_bfd);
  556.  
  557.   if (!bfd_check_format (sym_bfd, bfd_object))
  558.     error ("\"%s\": can't read symbols: %s.",
  559.        name, bfd_errmsg (bfd_error));
  560.  
  561.   return sym_bfd;
  562. }
  563.  
  564. /* Link a new symtab_fns into the global symtab_fns list.
  565.    Called by various _initialize routines.  */
  566.  
  567. void
  568. add_symtab_fns (sf)
  569.      struct sym_fns *sf;
  570. {
  571.   sf->next = symtab_fns;
  572.   symtab_fns = sf;
  573. }
  574.  
  575.  
  576. /* Initialize to read symbols from the symbol file sym_bfd.  It either
  577.    returns or calls error().  The result is a malloc'd struct sym_fns
  578.    that contains cached information about the symbol file.  */
  579.  
  580. static struct sym_fns *
  581. symfile_init (sym_bfd)
  582.      bfd *sym_bfd;
  583. {
  584.   struct sym_fns *sf, *sf2;
  585.  
  586.   for (sf = symtab_fns; sf != NULL; sf = sf->next)
  587.     {
  588.       if (!strncmp (bfd_get_target (sym_bfd), sf->sym_name, sf->sym_namelen))
  589.     {
  590.       sf2 = (struct sym_fns *)xmalloc (sizeof (*sf2));    
  591.       /* FIXME, who frees this? */
  592.       *sf2 = *sf;
  593.       sf2->sym_bfd = sym_bfd;
  594.       sf2->sym_private = 0;            /* Not alloc'd yet */
  595.       (*sf2->sym_init) (sf2);
  596.       return sf2;
  597.     }
  598.     }
  599.   error ("I'm sorry, Dave, I can't do that.  Symbol format unknown.");
  600.   return 0; /* Appease lint.  */
  601. }
  602.  
  603. /* This function runs the load command of our current target.  */
  604.  
  605. void
  606. load_command (arg, from_tty)
  607.      char *arg;
  608.      int from_tty;
  609. {
  610.   target_load (arg, from_tty);
  611. }
  612.  
  613. /* This function allows the addition of incrementally linked object files.
  614.    It does not modify any state in the target, only in the debugger.  */
  615.  
  616. /* ARGSUSED */
  617. void
  618. add_symbol_file_command (arg_string, from_tty)
  619.      char *arg_string;
  620.      int from_tty;
  621. {
  622.   char *name;
  623.   CORE_ADDR text_addr;
  624.   
  625.   /* Getting new symbols may change our opinion about what is
  626.      frameless.  */
  627.   reinit_frame_cache ();
  628.  
  629.   if (arg_string == 0)
  630.     error ("add-symbol-file takes a file name and an address");
  631.  
  632.   arg_string = tilde_expand (arg_string);
  633.   make_cleanup (free, arg_string);
  634.  
  635.   for( ; *arg_string == ' '; arg_string++ );
  636.   name = arg_string;
  637.   for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
  638.   *arg_string++ = (char) 0;
  639.  
  640.   if (name[0] == 0)
  641.     error ("add-symbol-file takes a file name and an address");
  642.  
  643.   text_addr = parse_and_eval_address (arg_string);
  644.  
  645.   dont_repeat ();
  646.  
  647.   if (from_tty) {
  648.     if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
  649.         name, local_hex_string (text_addr)))
  650.       error ("Not confirmed.");
  651.   }
  652.  
  653.   symbol_file_add (name, 0, text_addr, 0);
  654. }
  655.  
  656. /* Re-read symbols if the symbol-file has changed.  */
  657. void
  658. reread_symbols ()
  659. {
  660.   struct stat symstat;
  661.  
  662.   /* With the addition of shared libraries, this should be modified,
  663.      the load time should be saved in the partial symbol tables, since
  664.      different tables may come from different source files.  FIXME.
  665.      This routine should then walk down each partial symbol table
  666.      and see if the symbol table that it originates from has been changed
  667.   */
  668.  
  669.   if (stat (symfile, &symstat) < 0)
  670.     /* Can't read symbol-file.  Assume it is up to date.  */
  671.     return;
  672.  
  673.   if (symstat.st_mtime > symfile_mtime)
  674.     {
  675.       printf_filtered ("Symbol file has changed; re-reading symbols.\n");
  676.       symbol_file_command (symfile, 0);
  677.       breakpoint_re_set ();
  678.     }
  679. }
  680.  
  681. /* This function is really horrible, but to avoid it, there would need
  682.    to be more filling in of forward references.  */
  683. void
  684. fill_in_vptr_fieldno (type)
  685.      struct type *type;
  686. {
  687.   if (TYPE_VPTR_FIELDNO (type) < 0)
  688.     {
  689.       int i;
  690.       for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
  691.     {
  692.       fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
  693.       if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
  694.         {
  695.           TYPE_VPTR_FIELDNO (type)
  696.         = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
  697.           TYPE_VPTR_BASETYPE (type)
  698.         = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
  699.           break;
  700.         }
  701.     }
  702.     }
  703. }
  704.  
  705. /* Functions to handle complaints during symbol reading.  */
  706.  
  707. /* How many complaints about a particular thing should be printed before
  708.    we stop whining about it?  Default is no whining at all, since so many
  709.    systems have ill-constructed symbol files.  */
  710.  
  711. static unsigned stop_whining = 0;
  712.  
  713. /* Print a complaint about the input symbols, and link the complaint block
  714.    into a chain for later handling.  Result is 1 if the complaint was
  715.    printed, 0 if it was suppressed.  */
  716.  
  717. int
  718. complain (complaint, val)
  719.      struct complaint *complaint;
  720.      char *val;
  721. {
  722.   complaint->counter++;
  723.   if (complaint->next == 0) {
  724.     complaint->next = complaint_root->next;
  725.     complaint_root->next = complaint;
  726.   }
  727.   if (complaint->counter > stop_whining)
  728.     return 0;
  729.   wrap_here ("");
  730.   if (!info_verbose) {
  731.     puts_filtered ("During symbol reading...");
  732.   }
  733.   printf_filtered (complaint->message, val);
  734.   puts_filtered ("...");
  735.   wrap_here("");
  736.   if (!info_verbose)
  737.     puts_filtered ("\n");
  738.   return 1;
  739. }
  740.  
  741. /* Clear out all complaint counters that have ever been incremented.  */
  742.  
  743. void
  744. clear_complaints ()
  745. {
  746.   struct complaint *p;
  747.  
  748.   for (p = complaint_root->next; p != complaint_root; p = p->next)
  749.     p->counter = 0;
  750. }
  751.  
  752. /* allocate_symtab:
  753.  
  754.    Allocate and partly initialize a new symbol table.  Return a pointer
  755.    to it.  error() if no space.
  756.  
  757.    Caller must set these fields:
  758.     LINETABLE(symtab)
  759.     symtab->blockvector
  760.     symtab->dirname
  761.     symtab->free_code
  762.     symtab->free_ptr
  763.     initialize any EXTRA_SYMTAB_INFO
  764.     possibly free_named_symtabs (symtab->filename);
  765.     symtab->next = symtab_list;
  766.     symtab_list = symtab;
  767.  */
  768.  
  769. struct symtab *
  770. allocate_symtab(name)
  771.     char *name;
  772. {
  773.   register struct symtab *symtab;
  774.   char *c;
  775.  
  776.   symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
  777.   bzero (symtab, sizeof (*symtab));
  778.   symtab->filename = name;
  779.   symtab->fullname = NULL;
  780.   symtab->nlines = 0;
  781.   symtab->line_charpos = 0;
  782.   symtab->version = 0;
  783.   symtab->language = language_unknown;        /* default */
  784.  
  785.   c = rindex (name, '.');
  786.   
  787.   if (!c) {
  788.      ; /* Don't know language of file. */
  789.   } else if(!strcmp(c,".mod")) {
  790.      symtab->language = language_m2;
  791.   } else if(!strcmp(c,".c") || !strcmp(c,".cc")) {
  792.      symtab->language = language_c;
  793.   }
  794.  
  795.   return symtab;
  796. }
  797.  
  798. /* clear_symtab_users_once:
  799.  
  800.    This function is run after symbol reading, or from a cleanup.
  801.    If an old symbol table was obsoleted, the old symbol table
  802.    has been blown away, but the other GDB data structures that may 
  803.    reference it have not yet been cleared or re-directed.  (The old
  804.    symtab was zapped, and the cleanup queued, in free_named_symtab()
  805.    below.)
  806.  
  807.    This function can be queued N times as a cleanup, or called
  808.    directly; it will do all the work the first time, and then will be a
  809.    no-op until the next time it is queued.  This works by bumping a
  810.    counter at queueing time.  Much later when the cleanup is run, or at
  811.    the end of symbol processing (in case the cleanup is discarded), if
  812.    the queued count is greater than the "done-count", we do the work
  813.    and set the done-count to the queued count.  If the queued count is
  814.    less than or equal to the done-count, we just ignore the call.  This
  815.    is needed because reading a single .o file will often replace many
  816.    symtabs (one per .h file, for example), and we don't want to reset
  817.    the breakpoints N times in the user's face.
  818.  
  819.    The reason we both queue a cleanup, and call it directly after symbol
  820.    reading, is because the cleanup protects us in case of errors, but is
  821.    discarded if symbol reading is successful.  */
  822.  
  823. static int clear_symtab_users_queued;
  824. static int clear_symtab_users_done;
  825.  
  826. static void
  827. clear_symtab_users_once ()
  828. {
  829.   /* Enforce once-per-`do_cleanups'-semantics */
  830.   if (clear_symtab_users_queued <= clear_symtab_users_done)
  831.     return;
  832.   clear_symtab_users_done = clear_symtab_users_queued;
  833.  
  834.   printf ("Resetting debugger state after updating old symbol tables\n");
  835.  
  836.   /* Someday, we should do better than this, by only blowing away
  837.      the things that really need to be blown.  */
  838.   clear_value_history ();
  839.   clear_displays ();
  840.   clear_internalvars ();
  841.   breakpoint_re_set ();
  842.   set_default_breakpoint (0, 0, 0, 0);
  843.   current_source_symtab = 0;
  844. }
  845.  
  846. /* Delete the specified psymtab, and any others that reference it.  */
  847.  
  848. static void
  849. cashier_psymtab (pst)
  850.      struct partial_symtab *pst;
  851. {
  852.   struct partial_symtab *ps, *pprev;
  853.   int i;
  854.  
  855.   /* Find its previous psymtab in the chain */
  856.   for (ps = partial_symtab_list; ps; ps = ps->next) {
  857.     if (ps == pst)
  858.       break;
  859.     pprev = ps;
  860.   }
  861.  
  862.   if (ps) {
  863.     /* Unhook it from the chain.  */
  864.     if (ps == partial_symtab_list)
  865.       partial_symtab_list = ps->next;
  866.     else
  867.       pprev->next = ps->next;
  868.  
  869.     /* FIXME, we can't conveniently deallocate the entries in the
  870.        partial_symbol lists (global_psymbols/static_psymbols) that
  871.        this psymtab points to.  These just take up space until all
  872.        the psymtabs are reclaimed.  Ditto the dependencies list and
  873.        filename, which are all in the psymbol_obstack.  */
  874.  
  875.     /* We need to cashier any psymtab that has this one as a dependency... */
  876. again:
  877.     for (ps = partial_symtab_list; ps; ps = ps->next) {
  878.       for (i = 0; i < ps->number_of_dependencies; i++) {
  879.     if (ps->dependencies[i] == pst) {
  880.       cashier_psymtab (ps);
  881.       goto again;        /* Must restart, chain has been munged. */
  882.     }
  883.       }
  884.     }
  885.   }
  886. }
  887.  
  888. /* If a symtab or psymtab for filename NAME is found, free it along
  889.    with any dependent breakpoints, displays, etc.
  890.    Used when loading new versions of object modules with the "add-file"
  891.    command.  This is only called on the top-level symtab or psymtab's name;
  892.    it is not called for subsidiary files such as .h files.
  893.  
  894.    Return value is 1 if we blew away the environment, 0 if not.
  895.  
  896.    FIXME.  I think this is not the best way to do this.  We should
  897.    work on being gentler to the environment while still cleaning up
  898.    all stray pointers into the freed symtab.  */
  899.  
  900. int
  901. free_named_symtabs (name)
  902.      char *name;
  903. {
  904.   register struct symtab *s;
  905.   register struct symtab *prev;
  906.   register struct partial_symtab *ps;
  907.   struct blockvector *bv;
  908.   int blewit = 0;
  909.  
  910.   /* We only wack things if the symbol-reload switch is set.  */
  911.   if (!symbol_reloading)
  912.     return 0;
  913.  
  914.   /* Some symbol formats have trouble providing file names... */
  915.   if (name == 0 || *name == '\0')
  916.     return 0;
  917.  
  918.   /* Look for a psymtab with the specified name.  */
  919.  
  920. again2:
  921.   for (ps = partial_symtab_list; ps; ps = ps->next) {
  922.     if (!strcmp (name, ps->filename)) {
  923.       cashier_psymtab (ps);    /* Blow it away...and its little dog, too.  */
  924.       goto again2;        /* Must restart, chain has been munged */
  925.     }
  926.   }
  927.  
  928.   /* Look for a symtab with the specified name.  */
  929.  
  930.   for (s = symtab_list; s; s = s->next)
  931.     {
  932.       if (!strcmp (name, s->filename))
  933.     break;
  934.       prev = s;
  935.     }
  936.  
  937.   if (s)
  938.     {
  939.       if (s == symtab_list)
  940.     symtab_list = s->next;
  941.       else
  942.     prev->next = s->next;
  943.  
  944.       /* For now, queue a delete for all breakpoints, displays, etc., whether
  945.      or not they depend on the symtab being freed.  This should be
  946.      changed so that only those data structures affected are deleted.  */
  947.  
  948.       /* But don't delete anything if the symtab is empty.
  949.      This test is necessary due to a bug in "dbxread.c" that
  950.      causes empty symtabs to be created for N_SO symbols that
  951.      contain the pathname of the object file.  (This problem
  952.      has been fixed in GDB 3.9x).  */
  953.  
  954.       bv = BLOCKLIST (s);
  955.       if (BLOCKLIST_NBLOCKS (bv) > 2
  956.       || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
  957.       || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
  958.     {
  959.       complain (&oldsyms_complaint, name);
  960.  
  961.       clear_symtab_users_queued++;
  962.       make_cleanup (clear_symtab_users_once, 0);
  963.       blewit = 1;
  964.     } else {
  965.       complain (&empty_symtab_complaint, name);
  966.     }
  967.  
  968.       free_symtab (s);
  969.     }
  970.   else
  971.     {
  972.       /* It is still possible that some breakpoints will be affected
  973.      even though no symtab was found, since the file might have
  974.      been compiled without debugging, and hence not be associated
  975.      with a symtab.  In order to handle this correctly, we would need
  976.      to keep a list of text address ranges for undebuggable files.
  977.      For now, we do nothing, since this is a fairly obscure case.  */
  978.       ;
  979.     }
  980.  
  981.   /* FIXME, what about the misc function vector? */
  982.   return blewit;
  983. }
  984.  
  985. void
  986. _initialize_symfile ()
  987. {
  988.  
  989.   add_com ("symbol-file", class_files, symbol_file_command,
  990.        "Load symbol table from executable file FILE.\n\
  991. The `file' command can also load symbol tables, as well as setting the file\n\
  992. to execute.");
  993.  
  994.   add_com ("add-symbol-file", class_files, add_symbol_file_command,
  995.    "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
  996. The second argument provides the starting address of the file's text.");
  997.  
  998.   add_com ("load", class_files, load_command,
  999.    "Dynamically load FILE into the running program, and record its symbols\n\
  1000. for access from GDB.");
  1001.  
  1002.   add_show_from_set
  1003.     (add_set_cmd ("complaints", class_support, var_uinteger,
  1004.           (char *)&stop_whining,
  1005.       "Set max number of complaints about incorrect symbols.",
  1006.           &setlist),
  1007.      &showlist);
  1008.  
  1009.   add_show_from_set
  1010.     (add_set_cmd ("symbol-reloading", class_support, var_boolean,
  1011.           (char *)&symbol_reloading,
  1012.       "Set dynamic symbol table reloading multiple times in one run.",
  1013.           &setlist),
  1014.      &showlist);
  1015.  
  1016.   obstack_init (symbol_obstack);
  1017.   obstack_init (psymbol_obstack);
  1018. }
  1019.